{include 'admin/layout/header_list'}
<link rel="stylesheet" href="__ADMIN_STATIC__/css/pop_window.css?random={extendSetting('random')}">
<div class="downLoadList">
    <h4>安装列表</h4>
    <div class="downLoadListTable">
        <table>
            <col>
            <col width="120px">
            <tr class="downloadList-item">
                <td class="th"><span>文件名</span></td>
                <td class="th"><span>状态</span></td>
            </tr>
            {if isset($request['incUp']) && $request['incUp']=="0"}
                {foreach $listData $key $val}
                <tr class="downloadList-item">
                    <td>
                        <p class="file" data-id="{$val['id']}" data-isover="0" data-exec="0" >{basename($val['completePackageUrl'])}</p>
                    </td>
                    <td><span class="status" >---</span></td>
                </tr>
                {/foreach}
            {else}
                {foreach $listData $key $val}
                <tr class="downloadList-item">
                    <td>
                        {if $val['packageUrl']}
                        <p class="file" data-id="{$val['id']}" data-isover="0" data-exec="0" >{basename($val['packageUrl'])}</p>
                        {else}
                        <p class="file" data-id="{$val['id']}" data-isover="0" data-exec="0" >{basename($val['packageDelMd5Url'])}</p>
                        {/if}
                    </td>
                    <td><span class="status" >---</span></td>
                </tr>
                {/foreach}
            {/if}
        </table>
    </div>
</div>

<input type="hidden" class="installIncUp" value="{if isset($request['incUp'])}{$request['incUp']}{/if}" />
<script>
    $(function (){
        installExec();
    });

    //一个一个安装
    function installExec (){
        var url="{YIFANG_ROUTE_PREFIX}/systemUpgrade/installFile";
        $(".downLoadList").find(".file").each(function (index,ele){
            var isOver=$(ele).data("isover");
            var exec=$(ele).data("exec");
            if(isOver=="0" && exec=="0"){
                //进行安装
                var dataId=$(ele).data("id");
                var incUp=$(".installIncUp").val();
                $(ele).data("exec","1");
                $.ajax({
                    processData:false,
                    contentType :false,
                    type: "get",
                    url: url+"?id="+dataId+"&incUp="+incUp,
                    async: false,
                    dataType: "jsonp",
                    timeout: 0,
                    success:function(data)
                    {
                        var conError=1;
                        if(typeof data == "undefined")
                        {
                            conError=2;
                        }
                        else
                        {
                            if(!(typeof data == "object"))
                            {
                                data=eval("("+data+")");
                            }
                        }
                        if(data.success){
                            //调用执行脚本
                            var ok=execScript();
                            if(ok){
                                $(ele).parents(".downloadList-item").find(".status").html("完成");
                                $(ele).data("isover","1");
                            }else{
                                $(ele).parents(".downloadList-item").find(".status").html("脚本执行失败");
                                $(ele).data("isover","1");
                            }
                        }else{
                            //安装失败
                            $(ele).parents(".downloadList-item").find(".status").html("失败");
                            $(ele).data("isover","1");
                        }
                    },
                    complete:function(xhr)
                    {
                        $(ele).data("exec","0");
                        installExec();
                    }
                });
                return false;
            }
        });
    }

    /**
     * 执行安装脚本
     */
    function execScript(){
        var url="{YIFANG_ROUTE_PREFIX}/systemUpgrade/execScript";
        var ok=false;
        $.ajax({
            processData:false,
            contentType :false,
            type: "get",
            url: url,
            async: false,
            dataType: "jsonp",
            timeout: 0,
            success:function(data)
            {
                var conError=1;
                if(typeof data == "undefined")
                {
                    conError=2;
                }
                else
                {
                    if(!(typeof data == "object"))
                    {
                        data=eval("("+data+")");
                    }
                }
                if(data.success){
                    ok=true;
                }
            },
            complete:function(xhr)
            {
            }
        });
        return ok;
    }
</script>
{include 'admin/layout/footer'}